package cn.cqautotest.cqviehelper.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import cn.cqautotest.cqviehelper.R;

public class InputNumberView extends RelativeLayout {

    public static final String TAG = InputNumberView.class.getSimpleName();
    //当前的数值
    private int mCurrentNumber = 0;
    private View mTvMinus;
    private EditText mEtValue;
    private View mTvPlus;
    private OnNumberChangeListener mOnNumberChangeListener = null;
    private OnNumberMaxListener mOnNumberMaxListener = null;
    private OnNumberMinListener mOnNumberMinListener = null;
    //最大值
    private int mMax;
    //最小值
    private int mMin;
    //步长（每次能加或减多少）
    private int mStep;
    //默认的数值
    private int mDefaultValue = 0;
    //禁用（默认为false）
    private boolean mDisable = false;
    //左边按钮的背景资源
    private int mLeftBtnBgRes;
    //右边按钮的背景资源
    private int mRightBtnBgRes;

    public InputNumberView(Context context) {
        this(context, null);
    }

    public InputNumberView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public InputNumberView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取相关属性
        initAttrs(context, attrs);
        initView(context);
        //设置事件
        setUpEvent();
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.InputNumberView);
        mMax = typedArray.getInt(R.styleable.InputNumberView_max, 0);
        mMin = typedArray.getInt(R.styleable.InputNumberView_min, 0);
        mStep = typedArray.getInt(R.styleable.InputNumberView_step, 1);
        mDefaultValue = typedArray.getInt(R.styleable.InputNumberView_defaultValue, 0);
        mCurrentNumber = mDefaultValue;
        mDisable = typedArray.getBoolean(R.styleable.InputNumberView_disable, false);
        mLeftBtnBgRes = typedArray.getResourceId(R.styleable.InputNumberView_leftBtnBackground, -1);
        mRightBtnBgRes = typedArray.getResourceId(R.styleable.InputNumberView_rightBtnBackground, -1);
        typedArray.recycle();
    }

    private void setUpEvent() {
        mTvMinus.setOnClickListener(v -> {
            mTvPlus.setEnabled(true);
            mCurrentNumber -= mStep;
            if (mMin != 0 && mCurrentNumber <= mMin) {
                v.setEnabled(false);
                mCurrentNumber = mMin;
                if (mOnNumberMinListener != null) {
                    mOnNumberMinListener.onNumberMin(mCurrentNumber);
                }
            }
            updateText();
        });
        mTvPlus.setOnClickListener(v -> {
            mTvMinus.setEnabled(true);
            mCurrentNumber += mStep;
            if (mMax != 0 && mCurrentNumber >= mMax) {
                v.setEnabled(false);
                mCurrentNumber = mMax;
                if (mOnNumberMaxListener != null) {
                    mOnNumberMaxListener.onNumberMax(mCurrentNumber);
                }
            }
            updateText();
        });
    }

    private void initView(Context context) {
        //以下代码功能一样
        //LayoutInflater.from(context).inflate(R.layout.input_number_view, this, true);
        //
        //View view = LayoutInflater.from(context).inflate(R.layout.input_number_view, this, false);
        //addView(view);
        //
        LayoutInflater.from(context).inflate(R.layout.input_number_view, this);
        //以上代码功能一样，都是把View添加到当前容器里
        mTvMinus = findViewById(R.id.tvMinus);
        mEtValue = findViewById(R.id.etValue);
        mTvPlus = findViewById(R.id.tvPlus);
        //初始化控件值
        updateText();
        //设置按钮背景资源
        updateBtnBgRes();
        setUpButtonEnabled();
        upButtonState();
    }

    public InputNumberView setUpButtonEnabled() {
        mTvMinus.setEnabled(!mDisable);
        mTvPlus.setEnabled(!mDisable);
        return this;
    }

    public InputNumberView upButtonState() {
        setMinusEnabled();
        setPlusEnabled();
        return this;
    }

    public InputNumberView setMinusEnabled() {
        mTvMinus.setEnabled(!(mCurrentNumber <= mMin));
        return this;
    }

    public InputNumberView setPlusEnabled() {
        mTvPlus.setEnabled(!(mCurrentNumber >= mMax));
        return this;
    }

    /**
     * 更新所有按钮的背景资源（左边和右边）
     */
    public void updateBtnBgRes() {
        updateLeftBtnBgRes();
        updateRightBtnBgRes();
    }

    /**
     * 更新左边按钮的背景资源
     */
    public void updateRightBtnBgRes() {
        if (mLeftBtnBgRes == -1) return;
        mTvPlus.setBackgroundResource(mLeftBtnBgRes);
    }

    /**
     * 更新右边按钮的背景资源
     */
    public void updateLeftBtnBgRes() {
        if (mRightBtnBgRes == -1) return;
        mTvPlus.setBackgroundResource(mRightBtnBgRes);
    }

    public int getNumber() {
        return mCurrentNumber;
    }

    public InputNumberView setNumber(int value) {
        mCurrentNumber = value;
        return this;
    }

    public int getMax() {
        return mMax;
    }

    public InputNumberView setMax(int max) {
        mMax = max;
        return this;
    }

    public int getMin() {
        return mMin;
    }

    public InputNumberView setMin(int min) {
        mMin = min;
        return this;
    }

    public int getStep() {
        return mStep;
    }

    public InputNumberView setStep(int step) {
        mStep = step;
        return this;
    }

    public int getDefaultValue() {
        return mDefaultValue;
    }

    public InputNumberView setDefaultValue(int defaultValue) {
        mDefaultValue = defaultValue;
        mCurrentNumber = mDefaultValue;
        updateText();
        return this;
    }

    public boolean isDisable() {
        return mDisable;
    }

    public InputNumberView setDisable(boolean disable) {
        mDisable = disable;
        return this;
    }

    public int getLeftBtnBgRes() {
        return mLeftBtnBgRes;
    }

    /**
     * 设置左边按钮的背景资源（减号按钮）
     *
     * @param leftBtnBgRes
     */
    public InputNumberView setLeftBtnBgRes(int leftBtnBgRes) {
        mLeftBtnBgRes = leftBtnBgRes;
        updateLeftBtnBgRes();
        return this;
    }

    public int getRightBtnBgRes() {
        return mRightBtnBgRes;
    }

    /**
     * 设置右边按钮的背景资源（加号按钮）
     *
     * @param rightBtnBgRes
     */
    public InputNumberView setRightBtnBgRes(int rightBtnBgRes) {
        mRightBtnBgRes = rightBtnBgRes;
        updateRightBtnBgRes();
        return this;
    }

    /**
     * 更新数值
     */
    private void updateText() {
        mEtValue.setText(String.valueOf(mCurrentNumber));
        if (mOnNumberChangeListener != null) {
            mOnNumberChangeListener.onNumberChange(mCurrentNumber);
        }
    }

    /**
     * 设置数值改变时的监听
     *
     * @param listener
     */
    public void setOnNumberChangeListener(OnNumberChangeListener listener) {
        mOnNumberChangeListener = listener;
    }

    /**
     * 设置数值达到最大值时的监听
     *
     * @param listener
     */
    public void setOnNumberMaxListener(OnNumberMaxListener listener) {
        mOnNumberMaxListener = listener;
    }

    /**
     * 设置数值达到最小值时的监听
     *
     * @param listener
     */
    public void setOnNumberMinListener(OnNumberMinListener listener) {
        mOnNumberMinListener = listener;
    }

    public interface OnNumberChangeListener {
        void onNumberChange(int value);
    }

    public interface OnNumberMaxListener {
        void onNumberMax(int maxValue);
    }

    public interface OnNumberMinListener {
        void onNumberMin(int minValue);
    }
}
